var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    shopInfo: null,
    type: 0,
    searchValue: '',
    herbList: [],
    selectedHerbList:[],
    selectHerbsInfo: '',
    page: 1,
    limit: 10,
    totalPages: 1,
    hasLogin: false
  },
  init: function () {
    this.setData({
      herbList: [],
      page: 1,
      limit: 10,
      totalPages: 1
    });
  },
  getHerbGenericNameList(reachBottom = false) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.HerbGenericNameSelectList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      searchValue: that.data.searchValue
    }).then(function (res) {
      if (res.errno === 0) {
        let herbList = res.data.list;
        //触底加载新数据并保留老数据
        if (reachBottom) {
          herbList = [...that.data.herbList, ...herbList]
        }
        that.setData({
          herbList: herbList,
          totalPages: res.data.pages
        });
        that.initSelectHerbList();
        wx.hideLoading();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  onLoad: function (options) {},
  onReady: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.init();
    this.getHerbGenericNameList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
    } else {
      return false;
    }
    this.getHerbGenericNameList(true);
  },
  onShow: function () {
    if (app.globalData.hasLogin) {
      this.init();
      this.getHerbGenericNameList();

      let shopInfo = wx.getStorageSync('shopInfo');
      this.setData({
        shopInfo: shopInfo
      });
    }
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  onSearch() {
    this.getHerbGenericNameList();
  },
  checkedItem: function (event) {
    let itemIndex = event.currentTarget.dataset.index;
    let that = this;
    let target = {};
    let tmpHerbData = this.data.herbList.map(function (element, index, array) {
      if (index == itemIndex) {
        element.checked = !element.checked;
        target.checked = element.checked;
        target.genericName = element.genericName;
      }
      return element;
    });
    
    let targetSelectedList = this.data.selectedHerbList;
    let selectedHerbList = this.data.selectedHerbList;
    let selectedTarget = selectedHerbList.find((t)=>t.genericName == target.genericName);
    if(selectedTarget && !target.checked){
      targetSelectedList = selectedHerbList.filter((t)=>t.genericName != target.genericName);
    }else if(!selectedTarget && target.checked){
      targetSelectedList.push({genericName:target.genericName});
    }
    
    wx.setStorageSync('auth_selectHerbList', targetSelectedList);

    that.setData({
      herbList: tmpHerbData,
      selectedHerbList:targetSelectedList
    });

    let selectHerbsInfo = this.getSelectHerbsInfo();
    that.setData({
      selectHerbsInfo: selectHerbsInfo
    });
  },
  initSelectHerbList() {
    let authSelectHerbList = wx.getStorageSync('auth_selectHerbList');
    if (authSelectHerbList) {
      let that = this;
      let tmpHerbData = this.data.herbList.map(function (element, index, array) {
        // if (authSelectHerbList.includes(element.genericName)) {
        //   element.checked = true;
        // }
        if (authSelectHerbList.find((t) => t.genericName == element.genericName)) {
          element.checked = true;
        }
        return element;
      });
      that.setData({
        herbList: tmpHerbData,
        selectedHerbList:authSelectHerbList
      });
      let selectHerbsInfo = this.getSelectHerbsInfo();
      that.setData({
        selectHerbsInfo: selectHerbsInfo
      });
    }
  },
  getSelectHerbsInfo(){
    let selectHerbsInfo = this.data.selectedHerbList.map((t) => t.genericName).join(',');
    return selectHerbsInfo;
  },
  // getSelectHerbList() {
  //   let selectHerbs = this.data.herbList.filter((t) => t.checked == true);
  //   if (selectHerbs.length > 0) {
  //     // return selectHerbs.map((t) => t.genericName);
  //     return selectHerbs.map((t) => {
  //       let item = {};
  //       item.genericName = t.genericName;
  //       return item;
  //     });
  //   }
  //   return selectHerbs;
  // },
  // updateParentHerbListData() {
  //   let that = this;
  //   let pages = getCurrentPages();
  //   let beforePage = pages[pages.length - 2]

  //   let selectHerbs = that.getSelectHerbList();
  //   let selectHerbsInfo = selectHerbs.join(',');

  //   beforePage.setData({
  //     herbList: selectHerbs,
  //     selectHerbsInfo: selectHerbsInfo
  //   })
  // },
  selectConfirm() {
    let that = this;
    let selectHerbs = this.data.selectedHerbList;
    if (selectHerbs.length <= 0) {
      wx.showModal({
        title: '提示信息',
        content: '您未选择品类',
        showCancel: false
      });
      return;
    }
    let selectHerbsInfo = selectHerbs.map((t) => t.genericName).join(',');

    var pages = getCurrentPages(); // 当前页面
    var beforePage = pages[pages.length - 2]; // 前一个页面
    wx.navigateBack({
      success: function () {
        beforePage.callbackSetSelectHerbList(selectHerbs, selectHerbsInfo); // 执行前一个页面的onLoad方法
      }
    });
  },
})